//
//  SelectPrivateClassViewController.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/24.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "SelectPrivateClassViewController.h"
#import "CoursesClassTableViewCell.h"
#import "NewAddPrivateViewController.h"

@interface SelectPrivateClassViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)NoDataView *noDataView;


@property(nonatomic,strong)NSMutableArray *courseArray;

@end

@implementation SelectPrivateClassViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = BackGroundColor;
    
    self.title = @"选择私教课程";
    
    [self createSubView];
    
    [self loadData];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [NotificationCenter addObserver:self selector:@selector(loadData) name:@"ReloadCourse" object:nil];
    
}

#pragma mark 创建子视图
-(void)createSubView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[CoursesClassTableViewCell class] forCellReuseIdentifier:@"CoursesClassTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = CSClearColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
        if (iPhoneX) {
            
            make.bottom.mas_equalTo(self.view).offset(-NavagationBarHeightIphoneX - 60);
        }
        else{
            
            make.bottom.mas_equalTo(self.view).offset(-NavagationBarHeight1 - 60);
        }
    }];
}


//获取数据
-(void)loadData{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    self.brandModel = [BrandModel brand];
    
    [params setObject:self.brandModel.brandID forKey:@"brandid"];
                
    [params setObject:@"私教" forKey:@"type"];
    
    [HTTPTool GET:Get_Course_List parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  self.dataSource = [NSMutableArray arrayWithArray:[responseObject objectForKey:@"CourseList"]];
                  
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  if (self.dataSource.count == 0) {
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView showInView:self.view];
                  }
                  
                  [self.tableView reloadData];
              }
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}


#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoursesClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoursesClassTableViewCell"];
    
    NSDictionary *courseDict = [self.dataSource objectAtIndex:indexPath.section];
    
    CourseModel *courseModel = [CourseModel mj_objectWithKeyValues:courseDict];
    
    cell.courseModel = courseModel;
    
    cell.selectBtn.hidden = NO;
    
    if ([self.teachArray containsObject:courseModel.ID]) {
        
        cell.selectBtnSelect = YES;
    }
    else{
        
        cell.selectBtnSelect = NO;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *courseDict = [self.dataSource objectAtIndex:indexPath.section];
    
    CourseModel *courseModel = [CourseModel mj_objectWithKeyValues:courseDict];
    
    if ([self.courseArray containsObject:courseModel.ID]) {
    
        [self.courseArray removeObject:courseModel.ID];
    }
    else{
     
        [self.courseArray addObject:courseModel.ID];
    }
    
    [tableView reloadData];
}


#pragma mark 右侧按钮点击
-(void)rightBtnClick{
    
    if (self.courseArray.count == 0) {
        
        [MBProgressHUD showText:@"请选择课程"];
        
        return;
    }
    
    NewAddPrivateViewController *newAddPrivateVC = [[NewAddPrivateViewController alloc] init];
    
    newAddPrivateVC.courseArray = self.courseArray;
    
    newAddPrivateVC.workId = self.workId;
    
    [self.navigationController pushViewController:newAddPrivateVC animated:YES];
}


-(void)dealloc{
    
    [NotificationCenter removeObserver:self];
}


#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
    }
    
    return _dataSource;
}

-(NSMutableArray *)teachArray{
    
    if (!_courseArray) {
        
        _courseArray = [[NSMutableArray alloc] init];
    }
    
    return _courseArray;
}

@end
